﻿<template>
  <div class="shichang-container">
    <el-dialog v-model="isShowDialog" :width="800" :height="1000" draggable="">
      <template #header>
      <div style="color: #fff">
        <!--<el-icon size="16" style="margin-right: 3px; display: inline; vertical-align: middle"> <ele-Edit /> </el-icon>-->
        <span>考试名称：{{ props.title }}</span>
      </div>
    </template>
    <el-button type="primary" icon="ele-Plus" @click="openAddShichang" v-auth="'shichang:save'"> 新增 </el-button>
    <el-button type="primary" icon="ele-Plus" @click="save" v-auth="'shichang:save'"> 保存 </el-button>
    <el-button type="primary" icon="ele-Plus" @click="autoShichangRenshuHandle" v-auth="'shichang:autoShichangRenshu'"> 自动设置试场人数 </el-button>
    
    <el-card class="full-table" shadow="hover" style="margin-top: 8px">
      <el-table
				:data="tableData"
				style="width: 100%"
				v-loading="loading"
				tooltip-effect="light"
				row-key="id"
				border="">
        <!-- <el-table-column type="index" label="序号" width="55" align="center"/>
        <el-table-column prop="kaoshiId" label="考试" width="120" show-overflow-tooltip="">
          <template #default="scope">
            <span>{{scope.row.kaoshiIdName}}</span>
            
          </template>
          
        </el-table-column> -->
        <el-table-column prop="shiChangNum" label="试场号" width="100" show-overflow-tooltip="">
          <template #default="scope">
            <el-input v-model="scope.row.shiChangNum" placeholder="请输入试场号" maxlength="64" clearable />
          </template>
        </el-table-column>
        <el-table-column prop="renshu" label="人数" width="100" show-overflow-tooltip="">
          <template #default="scope">
            <el-input v-model="scope.row.renshu" placeholder="请输入人数" maxlength="64" clearable />
          </template>
        </el-table-column>
        <el-table-column prop="address1" label="地址1" width="100" show-overflow-tooltip="">
          <template #default="scope">
            <el-input v-model="scope.row.address1" placeholder="请输入地址1" maxlength="64" clearable />
          </template>
        </el-table-column>
        <el-table-column prop="address2" label="地址2" width="200" show-overflow-tooltip="">
          <template #default="scope">
            <el-input v-model="scope.row.address2" placeholder="请输入地址2" maxlength="64" clearable />
          </template>
        </el-table-column>
        <el-table-column prop="weizhi" label="位置" width="100" show-overflow-tooltip="">
          <template #default="scope">
            <el-input v-model="scope.row.weizhi" placeholder="请输入位置" maxlength="64" clearable />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="80" align="center" fixed="right" show-overflow-tooltip="" v-if="auth('shichang:save')">
          <template #default="scope">
            <el-button icon="ele-Delete" size="small" text="" type="primary" @click="delShichang(scope.row)" v-auth="'shichang:save'"> 删除 </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </el-dialog>
  </div>
</template>

<script lang="ts" setup="" name="shichang">
  import { ref } from "vue";
  import { ElMessageBox, ElMessage } from "element-plus";
  import { auth } from '/@/utils/authFunction';
  import { getDictDataItem as di, getDictDataList as dl } from '/@/utils/dict-utils';
  import { formatDate } from '/@/utils/formatTime';


  import printDialog from '/@/views/system/print/component/hiprint/preview.vue'
  import editDialog from '/@/views/main/shichang/component/editDialog.vue'
  import { pageShichang, saveShichang, autoShichangRenshu } from '/@/api/main/shichang';
  import { getKaoshiKaoshiIdDropdown } from '/@/api/main/shichang';

	var props = defineProps({
      title: {
      type: String,
      default: "",
    }
	});

  const kaoshiId = ref(0);
  const isShowDialog = ref(false);

  const showAdvanceQueryUI = ref(false);
  const printDialogRef = ref();
  const editDialogRef = ref();
  const loading = ref(false);
  const tableData = ref<any>([]);
  const queryParams = ref<any>({});
  const tableParams = ref({
    page: 1,
    pageSize: 10,
    total: 0,
  });

  const printShichangTitle = ref("");
  const editShichangTitle = ref("");

  // 改变高级查询的控件显示状态
  const changeAdvanceQueryUI = () => {
    showAdvanceQueryUI.value = !showAdvanceQueryUI.value;
  }
  

  // 查询操作
  const handleQuery = async () => {
    loading.value = true;
    queryParams.value.kaoshiId = kaoshiId.value;
    var res = await pageShichang(Object.assign(queryParams.value, tableParams.value));
    tableData.value = res.data.result?.items ?? [];
    tableParams.value.total = res.data.result?.total;
    loading.value = false;
  };

  // 打开新增页面
  const openAddShichang = () => {
    //editShichangTitle.value = '添加试场';
    //editDialogRef.value.openDialog({});
    tableData.value.push({kaoshiId:kaoshiId.value});
  };

  // 打开打印页面
  const openPrintShichang = async (row: any) => {
    printShichangTitle.value = '打印试场';
  }
  
  // 打开编辑页面
  const openEditShichang = (row: any) => {
    editShichangTitle.value = '编辑试场';
    editDialogRef.value.openDialog(row);
  };

  // 删除
  const delShichang = (row: any) => {
    const index = tableData.value.indexOf(row);
    if (index !== -1) {
      tableData.value.splice(index, 1);
    }};

  const kaoshiKaoshiIdDropdownList = ref<any>([]); 
  const getKaoshiKaoshiIdDropdownList = async () => {
    let list = await getKaoshiKaoshiIdDropdown();
    kaoshiKaoshiIdDropdownList.value = list.data.result ?? [];
  };

    	// 打开弹窗
	const openDialog = async (row: any) => {
		// ruleForm.value = JSON.parse(JSON.stringify(row));
		// 改用detail获取最新数据来编辑
    //queryParams.value.kaoshiId = row.id;
    kaoshiId.value = row.id;
    handleQuery();
		isShowDialog.value = true;
	};

  const save = async() => {
    //editKaoshiXuekeTitle.value = '添加考试学科';
    //editDialogRef.value.openDialog({});
    await saveShichang({kaoshiId:kaoshiId.value,data:tableData.value})
    handleQuery();
    ElMessage.success("保存成功");
  };

  const autoShichangRenshuHandle = async () => {
    await autoShichangRenshu({kaoshiId:kaoshiId.value});
    handleQuery();
    ElMessage.success("自动设置试场人数成功");
  };

  getKaoshiKaoshiIdDropdownList();
  
  handleQuery();

  defineExpose({ openDialog });
</script>
<style scoped>
:deep(.el-ipnut),
:deep(.el-select),
:deep(.el-input-number) {
	width: 100%;
}
</style>

